//
//  WDWorkOrderItemFirstViewCell.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/8.
//

#import "WDWorkOrderItemFirstViewCell.h"
#import "WDArrivalServiceItemModel.h"

@interface WDWorkOrderItemFirstViewCell ()

@property (nonatomic, strong) WDArrivalServiceItemModel *model;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) CALayer *line;

/// 车牌号
@property (nonatomic, strong) UILabel *plateNumberLabel;

/// vin码
@property (nonatomic, strong) UILabel *vehicleVinLabel;

/// 更新时间
@property (nonatomic, strong) UILabel *updateTimeLabel;

/// 服务流程
@property (nonatomic, strong) UILabel *servFlowLabel;

/// 缩略图
@property (nonatomic, strong) UIImageView *thumalImgView;

/// 车系
@property (nonatomic, strong) UILabel *vehicleSalesLabel;

/// 车型
@property (nonatomic, strong) UILabel *modelNameLabel;

/// 工单号
@property (nonatomic, strong) UILabel *taskNoLabel;

/// 预约时间
@property (nonatomic, strong) UILabel *appointmentTimeLabel;

@end

@implementation WDWorkOrderItemFirstViewCell

- (void)initWithSubViewCell {
    [super initWithSubViewCell];
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    [self initWithBgView];
    
    CGRect frame = CGRectMake(8, 35, self.bgView.width - 8 * 2, 1);
    _line = [CALayer drawLine:self.bgView frame:frame lineColor:[UIColor colorWithHexString:@"#19C46E"]];
}


- (void)initWithBgView {
    if (!_bgView) {
        CGRect frame = CGRectMake(20, 10, kScreenW - 40, kWDWorkOrderItemFirstViewCellHeight - 25);
        _bgView = [[UIView alloc] initWithFrame:frame];
        [self.contentView addSubview:_bgView];
        _bgView.alpha = 1.0;
        
        // 创建一个圆角矩阵
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(8, 8)];
        
        // 创建一个CAShapeLayer并给它设置路径
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.path = maskPath.CGPath;
        
        // gradient
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.frame = _bgView.bounds;
        gl.mask = maskLayer;
        gl.startPoint = CGPointMake(1, 0.43);
        gl.endPoint = CGPointMake(0.48, 0.44);
        gl.colors = @[(__bridge id)[UIColor colorWithHexString:@"#D5FFEA"].CGColor, (__bridge id)[UIColor colorWithHexString:@"#FFFFFF"].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        [_bgView.layer addSublayer:gl];
        
        _bgView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1600].CGColor;
        _bgView.layer.shadowOffset = CGSizeMake(0,0);
        _bgView.layer.shadowOpacity = 1;
        _bgView.layer.shadowRadius = 6;
        _bgView.layer.cornerRadius = 8;
        
    }
}

/// 车牌号
- (void)initWithPlateNumberLabel {
    if (!_plateNumberLabel) {
        CGRect frame = CGRectMake(8, (self.line.top - 20) * 0.5, 0, 20);
        _plateNumberLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.plateNumber);
        float width = [str widthForFont:_plateNumberLabel.font] + 2;
        _plateNumberLabel.width = width;
        _plateNumberLabel.text = str;
    }
}

/// vin码
- (void)initWithVehicleVinLabel {
    if (!_vehicleVinLabel) {
        CGRect frame = self.plateNumberLabel.frame;
        _vehicleVinLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        NSString *vin = objectNull(self.model.vehicleVin);
        if (vin.length > 8) {
            vin = [self.model.vehicleVin substringFromIndex:self.model.vehicleVin.length - 8];
        }
        float width = [vin widthForFont:_plateNumberLabel.font] + 2;
        _vehicleVinLabel.left = self.plateNumberLabel.right + 12;
        _vehicleVinLabel.width = width;
        _vehicleVinLabel.text = vin;
    }
}

/// 更新时间
- (void)initWithUpdateTimeLabel {
    if (!_updateTimeLabel) {
        CGRect frame = self.vehicleVinLabel.frame;
        _updateTimeLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#333333"];
        }];
    }
    if (self.model) {
        NSString *str = [NSString stringWithFormat:@"更新：%@", objectNull(self.model.updateTime)];
        float width = [str widthForFont:_updateTimeLabel.font] + 2;
        _updateTimeLabel.width = width;
        _updateTimeLabel.left = self.bgView.width - 8 - width;
        _updateTimeLabel.text = str;
    }
}

/// 服务流程
- (void)initWithServFlowLabel {
    if (!_servFlowLabel) {
        CGRect frame = CGRectMake(self.bgView.width - 8 - 46, self.line.bottom + 10, 46, 20);
        _servFlowLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#F9AD3B"];
        }];
    }
    if (self.model) {
//        NSString *str = [EDSUtility getServFlowName:self.model.servFlow];
        NSString *str = objectNull(self.model.servFlowValue);
        // 查询类型 0当前工单 1历史工单
        if ([self.model.searchType integerValue] == 1 && [str isEqualToString:@"已取消"]) {
            str = @"已关闭";
        }
        if ([self.model.auditStatus integerValue] == 1) {
            if (self.model.applyType && [self.model.applyType integerValue] == 0) {
                str = @"已申请救援";
            } else if (self.model.applyType && [self.model.applyType integerValue] == 1) {
                str = @"已申请取消救援";
            } else if (self.model.applyType && [self.model.applyType integerValue] == 2) {
                str = @"已申请关闭工单";
            }
        }
        float width = [str widthForFont:self.servFlowLabel.font] + 2;
        self.servFlowLabel.width = width;
        self.servFlowLabel.left = self.bgView.width -  8 - width;
        self.servFlowLabel.text = str;
    }
}

/// 缩略图
- (void)initWithThumalImgView {
    if (!_thumalImgView) {
        CGRect frame = CGRectMake(8, self.line.bottom + 20, 86, 68);
        _thumalImgView = [[UIImageView alloc] initWithFrame:frame];
        [self.bgView addSubview:_thumalImgView];
    }
    if (self.model) {
        [_thumalImgView sd_setImageWithURL:[NSURL URLWithString:self.model.imgPath] placeholderImage:[UIImage imageNamed:@"ic_default_veh"]];
    }
}

/// 车系
- (void)initWithVehicleSalesLabel {
    if (!_vehicleSalesLabel) {
        CGRect frame = CGRectMake(self.thumalImgView.right + 8, 0, 0, 22);
        _vehicleSalesLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:16 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.vehicleSalesValue);
        float width = [str widthForFont:self.vehicleSalesLabel.font] + 2;
        self.vehicleSalesLabel.width = width;
        self.vehicleSalesLabel.centerY = self.servFlowLabel.centerY;
        self.vehicleSalesLabel.text = str;
    }
}

/// 车型
- (void)initWithModelNameLabel {
    if (!_modelNameLabel) {
        CGRect frame = _vehicleSalesLabel.frame;
        frame.size.height = 20;
        frame.origin.y = self.vehicleSalesLabel.bottom + 4;
        _modelNameLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#19C46E"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.modelName);
        float width = [str widthForFont:self.modelNameLabel.font] + 2;
        self.modelNameLabel.width = width;
        self.modelNameLabel.text = str;
    }
}

/// 工单号
- (void)initWithTaskNoLabel {
    if (!_taskNoLabel) {
        CGRect frame = CGRectMake(self.modelNameLabel.left, self.modelNameLabel.bottom + 1, self.bgView.width - self.modelNameLabel.left - 8, 18);
        _taskNoLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#888888"];
        }];
    }
    if (self.model) {
        _taskNoLabel.text = [NSString stringWithFormat:@"工单号：%@", objectNull(self.model.taskNo)];
    }
}

/// 预约时间
- (void)initWithAppointmentTimeLabel {
    if (!_appointmentTimeLabel) {
        CGRect frame = self.taskNoLabel.frame;
        frame.origin.y = self.thumalImgView.bottom - frame.size.height + 2;
        _appointmentTimeLabel = [UILabel createWithLabel:self.bgView frame:frame font:[UIFont systemFontOfSize:12 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#888888"];
        }];
    }
    if (self.model) {
        NSString *str = objectNull(self.model.appointmentTime);
        if ([str isEqualToString:@""]) {
            str = objectNull(self.model.createTime);
        }
        _appointmentTimeLabel.text = [NSString stringWithFormat:@"预约日期：%@", str];
    }
}


- (void)updateCellData:(id)cellEntity {
    self.model = cellEntity;
    
    /// 车牌号
    [self initWithPlateNumberLabel];

    /// vin码
    [self initWithVehicleVinLabel];

    /// 更新时间
    [self initWithUpdateTimeLabel];

    /// 服务流程
    [self initWithServFlowLabel];

    /// 缩略图
    [self initWithThumalImgView];

    /// 车系
    [self initWithVehicleSalesLabel];

    /// 车型
    [self initWithModelNameLabel];

    /// 工单号
    [self initWithTaskNoLabel];

    /// 预约时间
    [self initWithAppointmentTimeLabel];
}

@end
